﻿using System.Configuration;

namespace Software.Common.Util.AppConfig
{
    public static class UtilAppConfig
    {
        /// <summary>
        /// 添加键为keyName、值为keyValue的项
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="keyValue"></param>
        public static void AddItem(string keyName, string keyValue)
        {
            //添加配置文件的项，键为keyName，值为keyValue
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add(keyName, keyValue);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//刷新命名节，这样在下次检索它时将从磁盘重新读取它。
        }

        /// <summary>
        /// 判断键值为KeyName的项是否存在
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static bool ExistItem(string keyName)
        {
            //判断配置文件中是否存在键为keyName的项
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == keyName)
                {
                    //存在
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取键值为KeyName的项的值
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static string GetItem(string keyName)
        {
            //返回配置文件中键为keyName的项的值
            return ConfigurationManager.AppSettings[keyName];
        }

        /// <summary>
        ///  修改键为keyName的项的值
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="newKeyValue"></param>
        public static void UpdateItem(string keyName, string newKeyValue)
        {
            //修改配置文件中键为keyName的项的值
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (ExistItem(keyName))
            {
                config.AppSettings.Settings[keyName].Value = newKeyValue;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
            else
                AddItem(keyName, newKeyValue);

        }

        /// <summary>
        /// 删除配置文件键为keyName的项
        /// </summary>
        /// <param name="keyName"></param>
        public static void RemoveItem(string keyName)
        {
            //删除配置文件键为keyName的项
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove(keyName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// 读取App.config 文件
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string GetAppConfig(string strKey)
        {
            return GetItem(strKey);
        }

        /// <summary>
        /// 读取App.cofig中string类配置项
        /// </summary>
        /// <param name="appSetKey"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetAppConfig(string appSetKey, string defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSetKey];
            if (string.IsNullOrEmpty(result))
                return defaultValue;
            return result;
        }

        /// <summary>
        /// 读取App.cofig中bool类配置项
        /// </summary>
        /// <param name="appSetKey"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetAppSettingValue(string appSetKey, bool defaultValue)
        {
            string result = ConfigurationManager.AppSettings[appSetKey];
            if (string.IsNullOrEmpty(result))
                return defaultValue;
            if (bool.TryParse(result, out bool boolResult))
                return boolResult;
            return defaultValue;
        }

        /// <summary>
        /// 更新App.config 文件
        /// </summary>
        /// <param name="newKey"></param>
        /// <param name="newValue"></param>
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            if (ExistItem(newKey))
                UpdateItem(newKey, newValue);
            else
                AddItem(newKey, newValue);
        }

        public static void UpdateAppConfig(string appSetKey, bool newValue)
        {
            UpdateAppConfig(appSetKey, newValue.ToString());
        }

    }
}
